package com.dd.census.controller;

import com.dd.census.controller.common.BaseController;
import com.dd.census.entity.Attendance;
import com.dd.census.service.PerformanceService;
import com.dd.census.tools.SysException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("rest/performance")
public class PerformanceController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(ParameterController.class);

    @Autowired
    PerformanceService performanceService;

    @RequestMapping("calculation")
    public ResponseEntity<Object> calculation(@RequestBody Attendance attendance, HttpServletRequest request) {

        try {
            // 基本输入验证
            return this.doSuccess(request, performanceService.calculation(attendance));

        } catch (SysException e) {
            // 业务异常
            return doFaild(request, e);

        } catch (Exception e) {
            // 系统异常
            logger.error("Internal Server Error", e);
            return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("calculationDetails")
    public ResponseEntity<Object> calculationDetails(@RequestBody Attendance attendance, HttpServletRequest request) {

        try {
            // 基本输入验证
            if(attendance.getRoleId().equals("1")){
                return this.doSuccess(request, performanceService.admincalculationDetails(attendance));
            }else{
                return this.doSuccess(request, performanceService.calculationDetails(attendance));
            }
        } catch (SysException e) {
            // 业务异常
            return doFaild(request, e);
        } catch (Exception e) {
            // 系统异常
            logger.error("Internal Server Error", e);
            return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}